package dk.eclipse.android.badassapp.preferences;

import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.preference.PreferenceManager;
import dk.eclipse.android.badassapp.R;

/**
 * Helper class for handling the application preferences.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 *
 */
public class PreferenceHelper {

	private static final String FORMAT_DECIMAL = "%f";

	public static boolean getCreateDatabase(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        boolean value = sp.getBoolean(context.getString(R.string.prefCreateDatabase), true);
		return value;
	}
	public static int getPreferredWeapon(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		String key = context.getString(R.string.prefPreferredWeapon);
		String defaultValue = context.getString(R.string.prefPreferredWeaponDefaultValue);
        int value = Integer.parseInt(sp.getString(key, defaultValue));
		return value;
	}
	public static String getFighterName(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefFighterName);
		String defaultValue = context.getString(R.string.prefFighterNameDefaultValue);
		String value = sp.getString(key, defaultValue);
		return value;
	}
	public static int getFighterAttitude(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefFighterAttitude);
		int defaultValue = Integer.valueOf(context.getString(R.string.prefFighterAttitudeDefaultValue));
		int value = sp.getInt(key, defaultValue);
		return value;
	}
	public static void setCreateDatabase(Context context, boolean value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putBoolean(context.getString(R.string.prefCreateDatabase), value);
        edit.commit();
	}	
	public static void setPreferredWeapon(Context context, int value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putString(context.getString(R.string.prefPreferredWeapon), String.valueOf(value));
        edit.commit();
	}
	public static String getIntelligenceEmail(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefIntelligenceEmail);
		String defaultValue = context.getString(R.string.prefIntelligenceEmailDefaultValue);
		String value = sp.getString(key, defaultValue);
		return value;
	}

	public static long getTargetId(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefTargetId);
		String defaultValue = context.getString(R.string.prefTargetIdDefaultValue);
		long value = Long.valueOf( sp.getString(key, defaultValue));
		return value;
	}

	public static void setTargetId(Context context, long value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putString(context.getString(R.string.prefTargetId), String.valueOf(value));
        edit.commit();
	}


	public static double getTargetLatitude(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefTargetLatitude);
		String defaultValue = context.getString(R.string.prefTargetLatitudeDefaultValue);
		double value = Double.valueOf(sp.getString(key, defaultValue));
		return value;
	}

	public static void setTargetLatitude(Context context, double value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putString(context.getString(R.string.prefTargetLatitude), String.format(Locale.US, FORMAT_DECIMAL, value));
        edit.commit();
	}


	public static double getTargetLongitude(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefTargetLongitude);
		String defaultValue = context.getString(R.string.prefTargetLongitudeDefaultValue);
		double value = Double.valueOf(sp.getString(key, defaultValue));
		return value;
	}

	public static void setTargetLongitude(Context context, double value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putString(context.getString(R.string.prefTargetLongitude), String.format(Locale.US, FORMAT_DECIMAL, value));
        edit.commit();
	}

	public static void setTargetLocation(Context context, Location location) {
		setTargetLocation(context, location.getLatitude(), location.getLongitude());
	}
	
	public static void setTargetLocation(Context context, double latitude, double longitude) {
		setTargetLatitude(context, latitude);
		setTargetLongitude(context, longitude);
	}
	
	public static double getMyLatitude(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefMyLatitude);
		String defaultValue = context.getString(R.string.prefMyLatitudeDefaultValue);
		double value = Double.valueOf(sp.getString(key, defaultValue));
		return value;
	}

	public static void setMyLatitude(Context context, double value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putString(context.getString(R.string.prefMyLatitude), String.format(Locale.US, FORMAT_DECIMAL, value));
        edit.commit();
	}


	public static double getMyLongitude(Context context) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
		String key = context.getString(R.string.prefMyLongitude);
		String defaultValue = context.getString(R.string.prefMyLongitudeDefaultValue);
		double value = Double.valueOf(sp.getString(key, defaultValue));
		return value;
	}

	public static void setMyLongitude(Context context, double value) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
        Editor edit = sp.edit();
        edit.putString(context.getString(R.string.prefMyLongitude), String.format(Locale.US, FORMAT_DECIMAL, value));
        edit.commit();
	}

	public static void setMyLocation(Context context, Location location) {
		setMyLatitude(context, location.getLatitude());
		setMyLongitude(context, location.getLongitude());
	}

	public static void setMyLocation(Context context,double latitude, double longitude) {
		setMyLatitude(context, latitude);
		setMyLongitude(context, longitude);
	}
}
